package com.wn.access.controller;

import com.wn.access.component.NotifyProvider;
import com.wn.access.service.ITradeLogService;
import com.wn.access.utils.BaseResult;
import com.wn.common.model.dto.bank.BankCardAddDTO;
import com.wn.common.model.dto.bank.BankCardRequestDTO;
import com.wn.common.model.dto.trade.bid.BankBidDTO;
import com.wn.common.model.dto.trade.bid.BidBalanceDTO;
import com.wn.common.model.dto.trade.bid.BorrowRecordStatusDTO;
import com.wn.common.model.vo.bank.SignatureVo;
import com.wn.common.model.vo.borrow.BorrowRecordVo;
import com.wn.common.model.vo.pay.RepayVo;
import com.wn.common.model.vo.trade.bid.LoanVo;
import com.wn.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2023-03-01
 */
@Api(tags = "托管代理账户服务")
@RestController
@RequestMapping("/")
@Slf4j
public class BankAccessController {

    @Autowired
    private ITradeLogService tradeLogService;

    @Autowired
    private NotifyProvider notifyProvider;

    @ApiOperation("访问银行托管代理服务生成交易记录")
    @PostMapping("/bank/access/card")
    public Result<SignatureVo> addBankAccount(@RequestBody BankCardAddDTO bankCardAddDTO){

        SignatureVo signatureVo = tradeLogService.savelog(bankCardAddDTO);

        return Result.ok(signatureVo);

    }

    @ApiOperation("银行托管系统绑卡回调结果")
    @PostMapping(value = "/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDTO bankCardRequestDto){

        // 01  把trade-log 进行同步

        String reqCode = bankCardRequestDto.getReqCode();

        tradeLogService.updateSyncStatus(reqCode);
        log.info("日志更新了同步.....");

        //02  把银行的返回数据 给用户中心
        notifyProvider.sendMsg(bankCardRequestDto);
        log.info("使用mq模拟银行回调成功,{}",bankCardRequestDto);


        return "success";
    }

    @ApiOperation(value = "托管代理向银行托管系统同步借款数据")
    @PostMapping("/bank/access/snyc/borrow")
    public Result<String> syncBorrowRecord(@RequestBody BorrowRecordVo borrowRecordVo){

        //返回银行结果集
        //保存日志  返回结果
        BaseResult baseResult =
                tradeLogService.syncBorrowRecord(borrowRecordVo);

        Result<String> result=new Result<>();
        result.setCode(200);
        result.setMessage(baseResult.getRespMsg());
        return result;

    }

    @ApiOperation("获取机构账户余额信息")
    @GetMapping("/bank/access/bid/balance/{userCode}")
    Result<BidBalanceDTO> getBidBalance(@PathVariable("userCode") String userCode){
        BidBalanceDTO balanceDetailsDTO = tradeLogService.getBidBalance(userCode);
        return Result.ok(balanceDetailsDTO);
    }

    @ApiOperation("银行保存投标记录")
    @PostMapping("/bank/access/bid/add")
    Result<String> bankBidService(@RequestBody BankBidDTO bankBidDTO){
        log.info("trade服务远程发送数据过来:{}",bankBidDTO);
        BaseResult baseResult =
                tradeLogService.bankBidService(bankBidDTO);
        Result<String> result=new Result<>();
        result.setCode(200);
        result.setMessage(baseResult.getRespMsg());
        return result;
    }

    @ApiOperation(value = "审核借款项目放款")
    @PostMapping("/bank/access/confirm-loan")
    Result<String> confirmLoan(@RequestBody LoanVo loanVo){

        log.info("存管代理，审核借款项目放款，请求参数loanRequest：{}",loanVo);
        BaseResult baseResult  = tradeLogService.confirmLoan(loanVo);
        log.info("baseResult返回结果：{}",baseResult);

        Result<String> result=new Result<>();
        result.setCode(200);
        result.setMessage(baseResult.getRespMsg());
        return result;
    }

    @ApiOperation(value = "修改借款项目状态") // 放款：接口3，修借款的状态
    @ApiImplicitParam(value = "修改借款项目状态",paramType = "body")
    @PostMapping("/bank/access/borrowRecord/status")
    Result<String> updateBorrowRecordStatus(
            @RequestBody BorrowRecordStatusDTO borrowRecordStatusDTO){

        log.info("存管代理，借款项目状态的修改，请求参数loanRequest：{}",borrowRecordStatusDTO);
        BaseResult baseResult  = tradeLogService.updateBorrowRecordStatus(borrowRecordStatusDTO);
        log.info("baseResult返回结果：{}",baseResult);

        Result<String> result=new Result<>();
        result.setCode(200);
        result.setMessage(baseResult.getRespMsg());
        return result;
    }

    @ApiOperation(value = "确认还款")
    @PostMapping("/bank/access/confirm/repay")
    Result<String> confirmRepay(
            @RequestBody RepayVo repayVo){

        log.info("还款 repayVo：{}",repayVo);
        BaseResult baseResult  = tradeLogService.confirmRepay(repayVo);
        log.info("baseResult返回结果：{}",baseResult);

        Result<String> result=new Result<>();
        result.setCode(200);
        result.setMessage(baseResult.getRespMsg());
        return result;
    }
}
